package com.rao.crm.web.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.rao.crm.domain.Customer;
import com.rao.crm.domain.LinkMan;
import com.rao.crm.domain.PageBean;
import com.rao.crm.service.CustomerService;
import com.rao.crm.service.LinkManService;
import jdk.internal.dynalink.linker.LinkerServices;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import java.util.List;
import java.util.PrimitiveIterator;

/**
 * @author Srao
 * @version 1.0
 * @className LinkManAction
 * @date 2019/9/20 21:22
 * @package com.rao.crm.web.action
 * @description
 */
public class LinkManAction extends ActionSupport implements ModelDriven<LinkMan> {
    private LinkMan linkMan = new LinkMan();
    private LinkManService linkManService;
    private CustomerService customerService;
    private Integer currPage = 1;
    private Integer pageSize = 3;
    @Override
    public LinkMan getModel() {
        return linkMan;
    }
    public void setLinkManService(LinkManService linkManService) {
        this.linkManService = linkManService;
    }
    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    public void setCurrPage(Integer currPage) {
        if (null == currPage){
            currPage = 1;
        }
        this.currPage = currPage;
    }

    public void setPageSize(Integer pageSize) {
        if (null == pageSize){
            pageSize = 3;
        }
        this.pageSize = pageSize;
    }

    public String findAll(){
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(LinkMan.class);
        /*设置条件*/
        if (linkMan.getLkm_name() != null && !"".equals(linkMan.getLkm_name())){
            detachedCriteria.add(Restrictions.like("lkm_name", "%"+linkMan.getLkm_name()+"%"));
        }
        if (linkMan.getLkm_gender() != null && !"".equals(linkMan.getLkm_gender())){
            detachedCriteria.add(Restrictions.eq("lkm_gender", linkMan.getLkm_gender()));
        }
        PageBean<LinkMan> pageBean = linkManService.findAll(detachedCriteria, currPage, pageSize);
        ActionContext.getContext().getValueStack().push(pageBean);
        return "findAll";
    }

    public String saveUI(){
        List<Customer> list = customerService.findAll();
        ActionContext.getContext().getValueStack().set("list", list);
        return "saveUI";
    }

    public String save(){
        linkManService.save(linkMan);
        return "save";
    }

    public String edit(){
        /*先查询所有的客户*/
        List<Customer> list = customerService.findAll();
        /*再查询所有的联系人*/
        linkMan = linkManService.findById(linkMan.getLkm_id());
        /*把对象放到值栈当中*/
        ActionContext.getContext().getValueStack().set("list", list);
        ActionContext.getContext().getValueStack().push(linkMan);
        return "edit";
    }

    public String update(){
        linkManService.update(linkMan);
        return "update";
    }

    public String delete(){
        /*先查询，后删除*/
        linkMan = linkManService.findById(linkMan.getLkm_id());
        linkManService.delete(linkMan);
        return "delete";
    }
}
